<?php
require_once(dirname(__FILE__) . '/app.php');

if(!$INI['db']['host']){
	redirect( WEB_ROOT . '/install.php' );
}

$request_uri = 'index';
$currentUrl = '/login.php';

$defaultShopId = $_GET['shop_id'];
if( $defaultShopId < 1 ){
	$defaultShopId = 1;
}

$tmp_date=date("YmdHi");
$tmp_year=substr($tmp_date,0,4) * 1;
$tmp_mon =substr($tmp_date,4,2) * 1;
$tmp_day = substr($tmp_date, 6, 2) * 1;
$tmp_Hour = substr($tmp_date, 8, 2) * 1;
$tmp_Minute = substr($tmp_date, 10, 2) * 1;

$searchDate = $_GET['search_date'];
$getDateArray = array();
if( preg_match("#/#", $searchDate) ){
	$getDateArray = explode("/", $searchDate);
}else if( preg_match("#-#", $searchDate) ){
	$getDateArray = explode("-", $searchDate);
}else{
	array_push($getDateArray, substr($searchDate, 0, 4));
	array_push($getDateArray, substr($searchDate, 4, 2));
	array_push($getDateArray, substr($searchDate, 6, 2));
}

$year = $getDateArray[0] * 1;
$month = $getDateArray[1] * 1;
$day = $getDateArray[2] * 1;

$year = $year < 1 ? $tmp_year : $year;
$month = $month < 1 ? $tmp_mon : $month;
$day = $day < 1 ? $tmp_day : $day;

$shopList = Shop::GetAllShops();
$shop = Shop::getById($defaultShopId);

$countList = Preengage::GetOrderCount($defaultShopId, $startTime, $endTime);
$countServiceIdList = array();

$serviceList = Service::GetAllByShopId($defaultShopId);
$serviceId = $_GET['service_id'];
$service = null;
if( $serviceId < 1 ){
	$serviceId = $serviceList[0]['id'];
	$service = $serviceList[0];
}else{
	$service = Service::GetById($serviceId);
}


$staffList = Staff::GetAllByShopAndService($defaultShopId, $serviceId);
$canNotOrderLimit = array();
$shopStartTime = mktime($shop['open_hour'], $shop['open_minute'], 0, $month, $day, $year);
$shopEndTime = mktime($shop['close_hour'], $shop['close_minute'], 0, $month, $day, $year);

foreach ($staffList as $value) {
	$tempList = array();
	$iniTemp = false;
	$temp = WorkSchedule::GetByYMD($value['id'], $year, $month, $day);
	if( $temp == null ){
		array_push( $tempList, array('start' => $shopStartTime, 'end' => $shopEndTime) );
		array_push($canNotOrderLimit, $tempList);
		continue;
	}
	//echo " start ....hahah".date("y-m-d H:i",$shopStartTime)."||".date("y-m-d H:i",$temp['start_time'])."<br>";
	if( $temp['start_time'] >  $shopStartTime){
		//echo " start ....hahah";
		array_push( $tempList, array('start' => $shopStartTime, 'end' => $temp['start_time']) );
		$iniTemp = true;
	}
	$orderList = Preengage::GetListShopIdAndStaffId($defaultShopId, $year, $month, $day, 0, 0, 23, 59, $value['id']);
	$rest = Absence::GetByYMD($value['id'], $year, $month, $day);
	$hasAddRest = false;
	foreach ($orderList as $value1) {
		if( !$hasAddRest ){
			if( $rest != null && $rest['start_time'] < $value1['service_start_time'] ){
				array_push( $tempList, array('start' => $rest['start_time'], 'end' => $rest['end_time']) );
				$iniTemp = true;
				$hasAddRest = true;
			}
		}
		array_push( $tempList, array('start' => $value1['service_start_time'], 'end' => $value1['service_end_time']) );
		$iniTemp = true;
	}
	if( !$hasAddRest && $rest != null ){
		array_push( $tempList, array('start' => $rest['start_time'], 'end' => $rest['end_time']) );
		$iniTemp = true;
	}
	
	if( $temp['end_time'] < $shopEndTime ){
		array_push( $tempList, array('start' => $temp['end_time'], 'end' => $shopEndTime) );
		$iniTemp = true;
	}
	if($iniTemp){
		array_push($canNotOrderLimit, $tempList);
	}else{
		array_push($canNotOrderLimit, array('start' => 0, 'end' => 0));
	}
}
$canNotOrderTime = array();
while( ($temp = array_pop($canNotOrderLimit) ) != null ){
	if( $temp[0]['start'] == 0 && $temp[0]['end'] == 0 ){
		break;
	}
	
	$temp1 = array_pop( $canNotOrderTime );
	if( $temp1 == null ){
		array_push($canNotOrderTime, $temp);
		//$canNotOrderTime = $temp;
		continue;
	}
	$tempList = array();
	$hasAddTemp = false;
	foreach ($temp as $k => $v){
		foreach ($temp1 as $k1 => $v1){
			//echo "!(".date("H:i", $v['start'])."  ".date("H:i", $v['end'])." || ".date("H:i", $v1['start'])."  ".date("H:i", $v1['end']).")<br/>";
			if( !($v['end'] < $v1['start'] || $v['start'] > $v1['end']) ){
				
				$subTempStartTime = $v['start'] > $v1['start'] ? $v['start'] : $v1['start'];
				$subTempEndTime = $v['end'] < $v1['end'] ? $v['end'] : $v1['end'];
				if( $subTempStartTime >= $subTempEndTime ){
					continue;
				}
				//echo "hahahha"."!(".date("y-m-d H:i", $subTempStartTime)."||".date("y-m-d H:i", $subTempEndTime)."<br/>";
				array_push($tempList, array( 'start' => $subTempStartTime, 'end' => $subTempEndTime ));
				$hasAddTemp = true;
			}
		}
	}
	if($hasAddTemp == true){
		array_push($canNotOrderTime, $tempList);
	}
	
}
$canNotOrderTime = array_pop($canNotOrderTime);
$canOrderTime = array(/*'start' => $shopStartTime, 'end' => $shopEndTime*/);

//$preCanNotOrder = null;
$hasAdded = false;

$hasAddedToEnd = false;
$startOver = false;
$lastOrderTime = $shopEndTime -  ($service['service_duration'] + $service['reset_device_time']) * 60;
foreach ( $canNotOrderTime as $k => $v ){
	//echo date('y-m-d H:i', $v['start'])."----".date('y-m-d H:i', $v['end']);
	//echo "<br/>\n";
	
	
	$tempStart = $v['start'] -  ($service['service_duration'] + $service['reset_device_time']) * 60;
	
	
	$temp = array_pop($canOrderTime);
	if( $tempStart < $shopStartTime ){
		//echo "tempStart <$tempStart> shopStartTime<$shopStartTime><br/>\n";
		if( $v['end'] >= $shopEndTime ){
			//echo "tempStart <$tempStart> shopStartTime<$shopStartTime><br/>\n".$v['end'];
			$canOrderTime = null;
			break;
		}
		//echo "tempStart111 <$tempStart> shopStartTime<$shopStartTime>||".date("y-m-d H:I", $v['end'])."||".date("y-m-d H:i", $shopEndTime)."<br>";
		$hasAdded = true;
		array_push($canOrderTime, array( 'start'=> $v['end'] + $service['reset_device_time'] * 60, 'end' => $lastOrderTime));
		$hasAddedToEnd = true;
		$startOver = true;
		//$preCanNotOrder = $v;
		continue;
	}
	if( $temp == null ){
		if( $startOver == false ){
			array_push($canOrderTime, array( 'start' => $shopStartTime, 'end' => $tempStart ));
			$startOver = true;
		}
		$hasAdded = true;
		//echo "hahah"."||".date("y-m-d H:i", $v['end'])."||".date("y-m-d H:i", $shopEndTime)."||";
		if( $v['end'] < $shopEndTime ){
			array_push($canOrderTime, array('start' => $v['end'] + $service['reset_device_time'] * 60, 'end' => $lastOrderTime));
		}
		continue;
	}
	
	
	//echo "hahahahah";
	if( $temp['end'] > $tempStart && ($temp['start'] + ($service['service_duration'] + $service['reset_device_time']) * 60 < $tempStart) ){
		//echo "hahahahah";
		array_push($canOrderTime, array('start' => $temp['start'], 'end' => $tempStart));
	}
	$hasAdded = true;
	if( $v['end'] < $lastOrderTime ){
		//echo "||".date("y-m-d H:i", $v['end'])."||".date("y-m-d H:i", $shopEndTime)."||";
		array_push($canOrderTime, array('start' => $v['end'] + $service['reset_device_time'] * 60, 'end' => $lastOrderTime));
	}
	
	
}

if( !$hasAdded ){
	array_push($canOrderTime, array('start' => $shopStartTime, 'end' => $lastOrderTime));
}

$tempCanOrderTime = array();
$canOrderIntarval = 10 * 60;
$now = time() + $canOrderIntarval;
foreach( $canOrderTime as $k => $v){
	$v['start'] = $v['start'];
	if( $v['end'] < $now ){
		continue;
	}
	if( $v['start'] < $now ){
		array_push($tempCanOrderTime, array('start' => $now, 'end' => $v['end']));
	}else{
		array_push($tempCanOrderTime, $v);
	}
}

$canOrderTime = $tempCanOrderTime;

$forenoonLimit = mktime(12, 0, 0, $month, $day, $year);
$afternoonLimit = mktime(17, 0, 0, $month, $day, $year);

$forenoonTime = null;
$afternoonTime = null;
$eveningTime = null;
foreach( $tempCanOrderTime as $k => $v){
	if( $v['start'] < $forenoonLimit && $forenoonTime == null){
		$tempEndLimit = mktime(date("H", $v['start']), 0, 0, $month, $day, $year);
		$tempEndTime = $tempEndLimit;
		if( $v['end'] < $tempEndLimit ){
			$tempEndTime = $v['end'];
		}
		$forenoonTime = array('start' => $v['start'], 'end' => $tempEndTime);
	}
	
	if( $v['start'] < $afternoonLimit && $v['end'] > $forenoonLimit && $afternoonTime == null){
		$tempStartTime = $forenoonLimit;
		if( $v['start'] > $forenoonLimit ){
			$tempStartTime = $v['start'];
		}
		$tempEndLimit = mktime(date("H", $tempStartTime) + 1, 0, 0, $month, $day, $year);
		$tempEndTime = $tempEndLimit;
		
		if( $v['end'] < $tempEndLimit ){
			$tempEndTime = $v['end'];
		}
		$afternoonTime = array('start' => $tempStartTime, 'end' => $tempEndTime);
	}
	if( $v['end'] > $afternoonLimit && $eveningTime == null){
		$tempStartTime = $afternoonLimit;
		if( $v['start'] > $afternoonLimit ){
			$tempStartTime = $v['start'];
		}
		$tempEndLimit = mktime(date("H", $tempStartTime) + 1, 0, 0, $month, $day, $year);
		$tempEndTime = $tempEndLimit;
		
		if( $v['end'] < $tempEndLimit ){
			$tempEndTime = $v['end'];
		}
		$eveningTime = array('start' => $tempStartTime, 'end' => $tempEndTime);
	}
	
}


/*foreach( $tempCanOrderTime as $k => $v ){
	if( $v['start'] )
}*/
//echo "haha<br/>\n";
//foreach ( $canOrderTime as $k => $v ){
//	echo "<br/>\n";
//	echo date('y-m-d H:i', $v['start'])."----".date('y-m-d H:i', $v['end']);
//}



include template('search');
